/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eSchool.Watcher;

import eSchool.BL.EntidadeBase;
import eSchool.Server.ISchoolServer;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 *
 * @author luis.zeferino
 */
public class Watcher extends UnicastRemoteObject implements ISchoolWatcher {

    ISchoolServer eServer;
    String host = "localhost";
    private ISchoolWatcherUI watcherUI;
    

    public Watcher() throws Exception {
        eServer = getMsgServer();
    }

    private ISchoolServer getMsgServer() throws Exception {
        int porto = 1099;
        //UnicastRemoteObject.exportObject(this,0); //Tem que ser feito no porto 0;
        Registry ror = LocateRegistry.getRegistry(host, porto);
        ISchoolServer server = (ISchoolServer) ror.lookup("eSchoolServer");
        return server;
    }

    public EntidadeBase logIn(String utilizador, String chave) throws RemoteException {
        return eServer.logIn(utilizador, chave);
    }

    public void logOut(String utilizador) throws RemoteException {
        eServer.logOut(utilizador);
    }

    public void logOut(EntidadeBase entidadeBase) throws RemoteException {
        eServer.logOut(entidadeBase);
    }

    public void novaNota(String text) throws RemoteException {
        watcherUI.receiveMsg( text);
    }
     
    public void receiveMsg(String server, String text) throws RemoteException {
        watcherUI.receiveMsg(server, text);
    }
    
     public ISchoolWatcherUI getChatUi() {
        return watcherUI;
    }

    public void setChatUi(ISchoolWatcherUI chatUi) {
        this.watcherUI = chatUi;
    }
}
